#!/usr/bin/perl
# remote-opt-get - command line apt interface
# Copyright 2006 by Pin-Shiun Chen (penkia) <penkia@gmail.com>
# This program is free software; 
# you can redistribute it and/or modify it under the same terms as Perl itself.
# See http://www.perl.com/perl/misc/Artistic.html

use strict;
use warnings;
use encoding 'utf8', STDIN =>'utf8', STDOUT =>'utf8';

BEGIN {
!system("sudo echo $ARGV[0] > /tmp/.opt-get-client") or die "$!\n";
}

our($filename, $path, $gladexml);

use Gtk2 '-init';
use Gtk2::GladeXML;
use File::Basename;

$gladexml = Gtk2::GladeXML->new('/usr/local/bin/ogc.glade');

&init();

$gladexml->signal_autoconnect_all(
'on_filechooserdialog1_destroy' => sub { Gtk2->main_quit },
'on_button1_clicked' => \&on_button1_clicked,
'on_button2_clicked' => sub { Gtk2->main_quit },
'on_button3_clicked' => sub { Gtk2->main_quit },
'on_dialog1_destroy' => sub { Gtk2->main_quit },
'on_dialog1_close' => sub { Gtk2->main_quit },
);

sub init {
chomp ($filename = basename(`cat /tmp/.opt-get-client`));
$gladexml->get_widget('filechooserdialog1')->set_current_name($filename);
$gladexml->get_widget('filechooserdialog1')->show;
}

sub on_button1_clicked {

$gladexml->get_widget('filechooserdialog1')->hide;

$path = $gladexml->get_widget('filechooserdialog1')->get_current_folder;

$ARGV[0] =~ s/op-get/ftp/;

!system("sudo wget $ARGV[0] -P $path") or die "$!\n";

$gladexml->get_widget('label2')->set_label(`sudo opt-get install $path/$filename`);

$gladexml->get_widget('dialog1')->show;

}


Gtk2->main; 
